package jp.co.showbiz.common.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;

/**
 * ｳｯﾂ･ ｰ・ﾃ util.
 */
public class DateUtil {

    private static String _defaultDateTimeOnlyFormat = "HH:mm:ss";

    private static String _dateFormat = "yyyy-MM-dd";

    private static String _slashDateFormat = "yyyy/MM/dd";

    private static String _dateTimeFormat = "yyyy/MM/dd HH:mm";

    private static String _timestampFormat = "yyyy-mm-dd HH:mm:ss";

    private static String _timestampFormatSlash = "yyyy/mm/dd HH:mm:ss";

    private static String[] _WeekDay = {"月", "火", "水", "木", "金", "土", "日" };

    public DateUtil() {
    }

    /**
     * ｺｸｰ晴ｭｿ・ﾀﾏｽﾃｸｦ ｸｮﾅﾏﾇﾑｴﾙ.<br>
     * ex) 2005/01/01(ﾈｭ) 10:59:59
     * @return String
     */
    public static String getDateTimeForReport() {
        String strRtDateTime = "";

        strRtDateTime = getTodayAsString(_slashDateFormat);
        strRtDateTime = strRtDateTime + "(" + getWeekToday(getDateInstance()) + ") "; // ｿ菎ﾏ -> (ﾈｭ)
        strRtDateTime = strRtDateTime + getTodayAsString(_defaultDateTimeOnlyFormat);

        return strRtDateTime;
    }

    /**
     * ｿﾀｴﾃﾀﾇ ｿ菎ﾏ ｹﾝﾈｯ
     * @param string Date
     * @return String
     */
    public static String getWeekToday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return _WeekDay[cal.get(Calendar.DAY_OF_WEEK) - 1];
    }

    /**
     * ｳｯﾂ･ 8ﾀﾚｸｮ -> 2005-11-1
     * @return String
     * @param strInput
     */
    public static String convertDate2Jcf(String strInput) {
        String dateJcf = "";
        if (strInput.length() != 8)
            return strInput;

        dateJcf = strInput.substring(0, 4) + "-" + strInput.substring(4, 6) + "-" + strInput.substring(6, 8);

        return dateJcf;
    }

    /**
     * Sets the date-time format property.
     * @param mValue the string to be set as the date-time format
     */
    public void setDateTimeFormat(String mValue) {
        _dateTimeFormat = mValue;
    }

    /**
     * Returns the date-time format.
     * @return a string representing the date-time format
     */
    public String getDateTimeFormat() {
        return _dateTimeFormat;
    }

    /**
     * Sets the date format property.
     * @param mMycfoDateFormat the string to be set as the date format
     */
    public void setDateFormat(String dateFormat) {
        _dateFormat = dateFormat;
    }

    /**
     * Returns the date format.
     * @return a string representing the date format
     */
    public String getDateFormat() {
        return _dateFormat;
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy-MM-dd ﾇ・ﾂﾀﾇ Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date(yyyy-MM-dd)
     */
    public static Date getDate(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_dateFormat);
        return dateform.parse(mDate);
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy/mm/dd HH:mm:ss ﾇ・ﾂﾀﾇ Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date(yyyy/mm/dd HH:mm:ss)
     */
    public static Date getDateTimeStamp(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_timestampFormatSlash);
        return dateform.parse(mDate);
    }

    /**
     * TimeStampﾇ・ｻ Dateﾇ・ﾂｷﾎ ｺｯﾈｯ
     * @param ts TimeStamp
     * @throws ParseException
     * @return Date
     */
    public static Date getDate(Timestamp ts) throws ParseException {

        if (null != ts)
            return new Date(ts.getTime());

        return null;
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy/MM/ddﾇ・ﾂﾀﾇ Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date(yyyy/MM/dd)
     */
    public static Date getDateFromSlash(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_slashDateFormat);
        return dateform.parse(mDate);
    }

    /**
     * "yyyy-mm-dd HH:mm:ss";
     * @param mDate
     * @return
     * @throws ParseException
     */
    public static Date getDateTimeStampMinus(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_timestampFormat);
        return dateform.parse(mDate);
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy/MM/dd HH:mmﾇ・ﾂﾀﾇ Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date(yyyy/MM/dd HH:mm)
     */
    public static Date getDateTime(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_dateTimeFormat);
        return dateform.parse(mDate);
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy-MM-ddﾇ・ﾂﾀﾇ java.sql.Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date
     */
    public static java.sql.Date getSQLDate(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_dateFormat);
        return new java.sql.Date(dateform.parse(mDate).getTime());
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy/MM/ddﾇ・ﾂﾀﾇ java.sql.Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param mDate String
     * @throws ParseException
     * @return Date
     */
    public static java.sql.Date getSQLDateBySlash(String mDate) throws ParseException {
        SimpleDateFormat dateform = new SimpleDateFormat(_slashDateFormat);
        return new java.sql.Date(dateform.parse(mDate).getTime());
    }

    /**
     * ｹｮﾀﾚｿｭ month,day,yearｸｦ ｹﾞｾﾆ Dateﾇ・ｸｷﾎ ｺｯﾈｯ<br>
     *<br>
     * Gets a Date object from three strings representing the month, day and year. If 'supressIncorrectDate' is false, any invalid combination of Month, Day and Year will yield a
     * IllegalArgumentException. Example: getDate("7", "33", "99", false) would result in a IllegalArgumentException. If 'supressIncorrectDate' is true an invalid combination of Month, Day and Year
     * will yield an adjusted date object. Example: getDate("7", "33", "99", false) would result in a Date object representing '08/02/99'. Observe that this functionality can only be guaranteed to the
     * extent it is supported by the java.util.Calendar class. Extreme cases such as getDate("999999999999999", "999999999999999", "999999999999", false) might yield unexpected results.
     * @param month a string representing the month
     * @param day a string representing the day
     * @param year a string representing the year
     * @param suppressIncorrectDate a boolean determining whether the method will be lenient on incorrect string parameters passed in
     * @return the date created from a string
     */
    public static Date getDate(String month, String day, String year, boolean suppressIncorrectDate) throws IllegalArgumentException {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setLenient(suppressIncorrectDate);
        Date d = null;
        c.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());
        d = c.getTime();
        return d;
    }

    /**
     * ﾇ鄲ﾏｷﾎｺﾎﾅﾍ amountOfDateｸｸﾅｭ ｿｬｻ・ﾈ Date ｹﾝﾈｯ
     * @param amountOfDate int
     * @return Date
     */
    public static Date getDateAfterToday(int amountOfDate) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, amountOfDate);
        return c.getTime();
    }

    /**
     * ｹｮﾀﾚｿｭ year,month,dayｸｦ ﾁｶﾇﾕﾇﾑ ﾀﾏﾀﾚｷﾎｺﾎﾅﾍ amountOfDateｸｸﾅｭ ｿｬｻ・ﾈ Date ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @param day String
     * @param amountOfDate int
     * @return Date
     */
    public static Date getDateAfterDay(String year, String month, String day, int amountOfDate) {
        Calendar c = Calendar.getInstance();

        c.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());

        c.add(Calendar.DAY_OF_YEAR, amountOfDate);
        return c.getTime();
    }

    /**
     * ﾀﾏﾀﾚｷﾎ ｺﾎﾅﾍ amountOfDateｸｸﾅｫ ｿｬｻ・ﾈ Dateｹﾝﾈｯ
     * @return : ｿｬｻ・ﾈ ﾀﾏﾀﾚ
     * @param amountOfDate : ｿｬｻ・ﾒ ﾀﾏｼ・
     * @param dt : ｱ簔ﾘ ﾀﾏﾀﾚ
     */
    public static Date getDateAfterDay(String dt, int amountOfDate) {
        if (dt.length() == 10)
            dt = dt.substring(0, 4) + dt.substring(5, 7) + dt.substring(8, 10);

        return getDateAfterDay(dt.substring(0, 4), dt.substring(4, 6), dt.substring(6, 8), amountOfDate);
    }

    /**
     * ﾇ鄲ﾏｷﾎｺﾎﾅﾍ amountOfDateｸｸﾅｭ ｿｬｻ・ﾈ Stringﾇ・ｳｯﾂ･ ｹﾝﾈｯ
     * @param amountOfDate int
     * @return String
     */
    public static String getDateAfterTodayAsString(int amountOfDate) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, amountOfDate);

        return getDateAsString(c.getTime());
    }

    /**
     * ｹｮﾀﾚｿｭ year,month,dayｸｦ ﾁｶﾇﾕﾇﾑ ﾀﾏﾀﾚｷﾎｺﾎﾅﾍ amountOfDateｸｸﾅｭ ｿｬｻ・ﾈ Stringﾇ・ｳｯﾂ･ ｺｯﾈｯ
     * @param year String
     * @param month String
     * @param day String
     * @param amountOfDate int
     * @return String
     */
    public static String getDateAfterDayAsSlashString(String year, String month, String day, int amountOfDate) {
        Calendar c = Calendar.getInstance();
        c.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());

        c.add(Calendar.DAY_OF_YEAR, amountOfDate);
        return getDateAsSlashString(c.getTime());
    }

    /**
     * ﾇﾘｴ・Dateｸｦ yyyy-MM-ddﾇ・ﾂﾀﾇ Stringﾇ・ｸｷﾎ ｺｯﾈｯ<br>
     * Gets a String version of the passed date in the format specified by dateFormat.
     * @param date the date to be converted to a string
     * @return a string version of the date passed in
     */
    public static String getDateAsString(Date date) {
        SimpleDateFormat dateform = new SimpleDateFormat(_dateFormat);
        return dateform.format(date);
    }

    /**
     * ﾇﾘｴ・Dateｸｦ dateFormatﾇ・ﾂﾀﾇ Stringﾇ・ｸｷﾎ ｺｯﾈｯ<br>
     * Gets a String version of the passed date in the format specified by dateFormat.
     * @param date the date to be converted to a string
     * @param dateFormat date format
     * @return a string version of the date passed in
     */
    public static String getDateAsString(Date date, String dateFormat) {
        SimpleDateFormat dateform = new SimpleDateFormat(dateFormat);
        return dateform.format(date);
    }

    /**
     * ｹｮﾀﾚｿｭﾀｻ yyyy/MM/ddﾇ・ﾂﾀﾇ Stringﾇ・ｸｷﾎ ｺｯﾈｯ<br>
     * @param date Date
     * @return String
     */
    public static String getDateAsSlashString(Date date) {
        SimpleDateFormat dateform = new SimpleDateFormat(_slashDateFormat);
        return dateform.format(date);
    }

    /**
     * ﾇﾘｴ鄲ﾏﾀﾚﾀﾇ ｿｬｵｵｸｦ Stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @param date Date
     * @return String
     */
    public static String getYearStrFromDate(Date date) {
        if (date == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.toString(cal.get(Calendar.YEAR));
    }

    /**
     * ﾇﾘｴ鄲ﾏﾀﾚﾀﾇ ｿｻ Stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @param date Date
     * @return String
     */
    public static String getMonthStrFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.toString(cal.get(Calendar.MONTH) + 1);
    }

    /**
     * ﾇﾘｴ鄲ﾏﾀﾚﾀﾇ ｳｯﾂ･ｸｦ Stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @param date Date
     * @return String
     */
    public static String getDayStrFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * nYear1,nMonth1,nDate1ﾀｻ ﾁｶﾇﾕﾇﾑ ｳｯﾂ･ｿﾍ nYear2,nMomth2,nDate2ｸｦ ﾁｶﾇﾕﾇﾑ ｳｯﾂ･ｿﾍﾀﾇ ﾂﾌﾀﾏｼ・
     * @param nYear1 int
     * @param nMonth1 int
     * @param nDate1 int
     * @param nYear2 int
     * @param nMonth2 int
     * @param nDate2 int
     * @return int
     */
    public static int GetDifferenceOfDate(int nYear1, int nMonth1, int nDate1, int nYear2, int nMonth2, int nDate2) {
        Calendar cal = Calendar.getInstance();
        int nTotalDate1 = 0, nTotalDate2 = 0, nDiffOfYear = 0, nDiffOfDay = 0;

        if (nYear1 > nYear2) {
            for (int i = nYear2; i < nYear1; i++) {
                cal.set(i, 12, 0);
                nDiffOfYear += cal.get(Calendar.DAY_OF_YEAR);
            }

            nTotalDate1 += nDiffOfYear;
        } else if (nYear1 < nYear2) {
            for (int i = nYear1; i < nYear2; i++) {
                cal.set(i, 12, 0);
                nDiffOfYear += cal.get(Calendar.DAY_OF_YEAR);
            }
            nTotalDate2 += nDiffOfYear;
        }

        cal.set(nYear1, nMonth1 - 1, nDate1);
        nDiffOfDay = cal.get(Calendar.DAY_OF_YEAR);
        nTotalDate1 += nDiffOfDay;

        cal.set(nYear2, nMonth2 - 1, nDate2);
        nDiffOfDay = cal.get(Calendar.DAY_OF_YEAR);
        nTotalDate2 += nDiffOfDay;

        return nTotalDate1 - nTotalDate2;
    }

    /**
     * ｵﾎ ﾀﾏﾀﾚｰ｣ ﾂﾌﾀﾏｼ・ｹﾝﾈｯ
     * @return int
     * @param frDt
     * @param toDt
     */
    public static int GetDifferenceOfDate(String toDt, String frDt) {
        if (frDt.length() < 8 || toDt.length() < 8)
            return -1;

        if (frDt.length() == 10)
            frDt = frDt.substring(0, 4) + frDt.substring(5, 7) + frDt.substring(8, 10);
        if (toDt.length() == 10)
            toDt = toDt.substring(0, 4) + toDt.substring(5, 7) + toDt.substring(8, 10);

        if (frDt.length() != 8 || toDt.length() != 8)
            return -1;

        return GetDifferenceOfDate(Integer.parseInt(toDt.substring(0, 4)), Integer.parseInt(toDt.substring(4, 6)), Integer.parseInt(toDt.substring(6, 8)), Integer.parseInt(frDt.substring(0, 4)),
                Integer.parseInt(frDt.substring(4, 6)), Integer.parseInt(frDt.substring(6, 8)));
    }

    /**
     * dateﾀﾇ ｽﾃｰ｣ｰｪﾀｻ Stringﾀｸｷﾎ ｹﾝﾈｯ
     * @param date Date
     * @return String
     */
    public static String getHourStrFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour < 10) {
            return "0" + Integer.toString(hour);
        } else {
            return Integer.toString(hour);
        }
    }

    /**
     * dateﾀﾇ ｺﾐﾀｻ Stringﾀｸｷﾎ ｹﾝﾈｯ
     * @param date Date
     * @return String
     */
    public static String getMinStrFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.toString(cal.get(Calendar.MINUTE));
    }

    /**
     * ｿﾀｴﾃｳｯﾂ･ｸｦ Stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @return String
     */
    public static String getTodayAsString() {
        Date date = new Date();
        SimpleDateFormat dateform = new SimpleDateFormat(_dateFormat);
        return dateform.format(date);
    }

    /**
     * ｿﾀｴﾃﾀﾏﾀﾚｸｦ ｱｸﾇﾑｴﾙ.
     * @return : Formatted Date String
     * @param format : Date String Format
     */
    public static String getTodayAsString(String format) {
        Date date = new Date();
        SimpleDateFormat dateform = new SimpleDateFormat(format);
        return dateform.format(date);
    }

    /**
     * parameter datetimeﾀｻ yyyy/MM/dd HH:mmﾇ・Stringﾀｸｷﾎ ｺｯﾈｯ<br>
     * Gets a String version of the passed date-time in the format specified by date-time Format.
     * @param date the date-time to be converted to a string
     * @return a string version of the date-time passed in
     */
    public static String getDateTimeAsString(Date datetime) {
        DateFormat dateformater = new SimpleDateFormat(_dateTimeFormat);
        return dateformater.format(datetime);
    }

    /**
     * ﾇ鄲ﾏﾀﾚﾀﾇ Dateｰｴﾃｼｸｦ ｹﾞｴﾂｴﾙ.
     * @return Date
     */
    public static Date getDateInstance() {
        Date date = (Date) currentDate.get();
        date.setTime(System.currentTimeMillis());
        return date;
    }

    /**
	 *
	 */
    @SuppressWarnings("unchecked")
	private static ThreadLocal currentDate = new ThreadLocal() {
        protected Object initialValue() {
            return new Date();
        }
    };

    /**
     * java.util.Dateﾇ・ｻ java.sql.Timestampﾇ・ｸｷﾎ ｺｯﾈｯ
     * @param datetime Date
     * @return Timestamp
     */
    public static java.sql.Timestamp getTimestamp(java.util.Date datetime) {

        if (null != datetime) {
            return new java.sql.Timestamp(datetime.getTime());
        } else {
            return null;
        }
    }

    /**
     * ﾇ鄲ﾏﾀｻ java.sql.Dateﾇ・ﾂｷﾎ ｺｯﾈｯ
     * @return Date
     */
    public static java.sql.Date getTodayAsSQLDate() {
        Date today = getDateInstance();
        return new java.sql.Date(today.getTime());
    }

    /**
     * ﾇﾘｴ鄲ﾏﾀｻ java.sql.Dateﾇ・ﾂｷﾎ ｺｯﾈｯ
     * @param datetime Date
     * @return Date
     */
    public static java.sql.Date getSQLDate(java.util.Date datetime) {
        if (null != datetime) {
            return new java.sql.Date(datetime.getTime());
        } else {
            return null;
        }
    }

    /**
     * ﾇﾘｴ鄲ﾏﾀｻ java.sql.Timestampﾇ・ﾂｷﾎ ｺｯﾈｯ
     * @param datetime Date
     * @return Timestamp
     */
    public static java.sql.Timestamp getSQLTimestamp(java.util.Date datetime) {
        if (null != datetime) {
            return new java.sql.Timestamp(datetime.getTime());
        } else {
            return null;
        }

    }

    /**
     * ｱﾝﾀﾏﾀﾌ ｼﾓﾇﾏｴﾂ ﾇﾘｴ鄙ﾇ ｽﾃﾀﾛﾀﾏﾀｻ Dateﾇ・ﾂｷﾎ ｹﾝﾈｯ
     * @throws ParseException
     * @return Date
     */
    public static Date firstDateOfThisMonth() throws ParseException {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String dateStr = year + "-" + month + "-01";
        return DateUtil.getDate(dateStr);
    }

    /**
     * ｱﾝﾁﾖﾀﾇ ｿ菎ﾏ Dateｸｦ ｹﾝﾈｯ
     * @return Date
     */
    public static Date getMondayOfThisWeek() {
        Calendar cal = Calendar.getInstance();
        int i = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK);
        Date date = getDateAfterToday(i);
        return date;
    }

    /**
     * ﾀﾔｷﾂｹﾞﾀｺ year,month,dayｸｦ ﾁｶﾇﾕﾇﾑ dateﾀﾇ ｿ菎ﾏ dateｸｦ ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @param day String
     * @return Date
     */
    public static Date getMondayOfWeek(String year, String month, String day) {
        Calendar cal = Calendar.getInstance();
        cal.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());

        int i = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, i);
        return cal.getTime();
    }

    /**
     * ﾀﾔｷﾂｹﾞﾀｺ year,month,dayｸｦ ﾁｶﾇﾕﾇﾑ dateﾀﾇ ｿ菎ﾏ dateｸｦ stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @param day String
     * @return String
     */
    public static String getMondayStrOfWeek(String year, String month, String day) {
        Calendar cal = Calendar.getInstance();
        cal.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());

        int i = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, i);
        return getDateAsString(cal.getTime());
    }

    /**
     * ﾀﾔｷﾂｹﾞﾀｺ year,month,dayｸｦ ﾁｶﾇﾕﾇﾑ dateﾀﾇ ﾀﾏｿ菎ﾏﾀｻ dateｸｦ stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @param day String
     * @return String
     */
    public static String getSundayStrOfWeek(String year, String month, String day) {
        Calendar cal = Calendar.getInstance();
        cal.set((Integer.valueOf(year)).intValue(), (Integer.valueOf(month)).intValue() - 1, (Integer.valueOf(day)).intValue());

        int i = Calendar.SUNDAY - cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DAY_OF_YEAR, i);
        return getDateAsString(cal.getTime());
    }

    /**
     * ｱﾝﾁﾖﾂﾇｿ菎ﾏ dateｸｦ stringﾇ・ｸｷﾎ ｹﾝﾈｯ
     * @return String
     */
    public static String getMondayStrOfThisWeek() {
        Calendar cal = Calendar.getInstance();
        int i = Calendar.MONDAY - cal.get(Calendar.DAY_OF_WEEK);
        String strDate = getDateAfterTodayAsString(i);
        return strDate;
    }

    /**
     * ｴﾙﾀｽｴﾞﾀﾇ ｽﾃﾀﾛﾀﾏ dateｸｦ ｹﾝﾈｯ
     * @throws ParseException
     * @return Date
     */
    public static Date firstDateOfNextMonth() throws ParseException {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        if (month == 12) {
            year++;
            month = 1;
        } else {
            month++;
        }
        String dateStr = year + "-" + month + "-01";
        return DateUtil.getDate(dateStr);
    }

    /**
     * ﾀﾔｷﾂｹﾞﾀｺ year,monthﾀﾇ ｽﾃﾀﾛﾀﾏ dateｸｦ ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @throws ParseException
     * @return Date
     */
    public static Date firstDateOfMonth(String year, String month) throws ParseException {
        String dateStr = year + "-" + month + "-01";
        return DateUtil.getDate(dateStr);
    }

    /**
     * ｴﾙﾀｽｴﾞﾀﾇ ｽﾃﾀﾛﾀﾏ dateｸｦ ｹﾝﾈｯ
     * @param year String
     * @param month String
     * @throws ParseException
     * @return Date
     */
    public static Date firstDateOfNextMonth(String year, String month) throws ParseException {
        if (month.equals("12")) {
            int temp = Integer.parseInt(year);
            temp++;
            year = Integer.toString(temp);
            month = "01";
        } else {
            int temp = Integer.parseInt(month);
            temp++;
            month = Integer.toString(temp);
        }
        String dateStr = year + "-" + month + "-01";
        return DateUtil.getDate(dateStr);
    }

    /**
     * @param year String
     * @param month String
     * @param chkday int
     * @throws ParseException
     * @return int
     */
    /*
     * public static int numberOfDay(String year, String month, int chkday) throws ParseException { int numberOfDay = 0; int lastDate = lastDateOfMonth(year, month); // 29 ~ 31 int firstday =
     * firstDateOfMonth(year, month).getDay(); // 0 ~ 6 int firstDayOfchkday; //chkdayｿ｡ ﾇﾘｴ酩ﾏｴﾂ ｱﾗ ｴﾞﾀﾇ ﾀﾏﾀﾚｸｦ ｱｸﾇﾑｴﾙ. //firstdayｿﾍ chkdayｰ｡ ｰｰﾀｺ ﾁﾖｶ・, chkday - firstday if (chkday >= firstday) {
     * firstDayOfchkday = (chkday - firstday) + 1; } else { firstDayOfchkday = 8 - (firstday - chkday); } numberOfDay = (lastDate - firstDayOfchkday) / 7 + 1; return numberOfDay; }
     */
    /**
     * strYear,strMonｿ｡ ﾇﾘｴ酩ﾏｴﾂ ｿﾇ ｸｶﾁｷﾀﾏﾀｻ ｱｸﾇﾑｴﾙ.
     * @param strYear String
     * @param strMon String
     * @return int
     */
    public static int lastDateOfMonth(String strYear, String strMon) throws ParseException {
        // ｴﾙﾀｽｴﾞ 1ﾀﾏｿ｡ｼｭ 1ﾀﾏ ｻｫｴﾙ
        Calendar c = Calendar.getInstance();

        c.set(Integer.parseInt(strYear), Integer.parseInt(strMon), 1);

        c.add(Calendar.DATE, -1);

        return c.get(Calendar.DAY_OF_MONTH);

        /*
         * int year = Integer.parseInt(strYear); int month = Integer.parseInt(strMon); switch (month) { case 1: //1,3,5,7,8,10,12ｿﾇ ｳｯﾂ･ｸｦ 31ﾀﾏｷﾎ ﾁ､ﾇﾑｴﾙ. case 3: case 5: case 7: case 8: case 10: case
         * 12: return (31); case 4: //4,6,9,11ｿﾇ ｳｯﾂ･ｸｦ 30ﾀﾏｷﾎ ﾁ､ﾇﾑｴﾙ. case 6: case 9: case 11: return (30); default: //ﾀｱｳ簑ｻ ｰﾋｻ酩ﾘｼｭ 2ｿﾇ ｳｯﾂ･ｸｦ 28 ｶﾇｴﾂ 29ﾀﾏｷﾎ ﾁ､ﾇﾑｴﾙ. if (year % 4 != 0) { return
         * (28); } else if (year % 100 != 0) { return (29); } else if (year % 400 != 0) { return (28); } else { return (29); } }
         */
    }

    /**
     * strYearMonth ｿ｡ ﾇﾘｴ酩ﾏｴﾂ ｿﾇ ｸｶﾁｷﾀﾏ YYYYMMDD ﾇ・ｸｷﾎ Return
     * @param strYearMonth String
     * @return String
     */
    public static String getLastDateStringOfMonth(String strYearMonth) throws ParseException {
        // ｴﾙﾀｽｴﾞ 1ﾀﾏｿ｡ｼｭ 1ﾀﾏ ｻｫｴﾙ
        Calendar c = Calendar.getInstance();

        c.set(Integer.parseInt(strYearMonth.substring(0, 4)), Integer.parseInt(strYearMonth.substring(4, 6)), 1);

        c.add(Calendar.DATE, -1);
        return getDateAsString(c.getTime(), "yyyyMMdd");
    }

    // written by jeuk
    /*
     * public static float getDiffHour(Timestamp regTime, Timestamp startTime, Timestamp endTime) { long lStart = startTime.getTime(); long lEnd = endTime.getTime(); long lReg = regTime.getTime();
     * String regYear = getYearStrFromDate(new Date(lReg)); String regMonth = getMonthStrFromDate(new Date(lReg)); try { Date firstDateOfMonth = firstDateOfMonth(regYear, regMonth); Date
     * firstDateOfNextMonth = firstDateOfNextMonth(regYear, regMonth); if (lStart < firstDateOfMonth.getTime()) { lStart = firstDateOfMonth.getTime(); } if (lEnd > firstDateOfNextMonth.getTime()) {
     * lEnd = firstDateOfNextMonth.getTime(); } } catch (Exception e) {} long diff; if (lEnd > lStart) { diff = lEnd - lStart; } else { diff = 0; } float returnValue = (float) (diff) / (1000.f 60.f
     * 60.f); System.out.println("**********"); System.out.println(startTime + "---" + endTime + "---" + returnValue); System.out.println(new Date(lStart) + "---" + new Date(lEnd)); return
     * returnValue; }
     */
    /**
     * year,monthｸｦ ﾁｶﾇﾕﾇﾑ ｿ・ｴﾙﾀｽｿﾇ ﾂﾌｸｦ ｽﾃｰ｣ ｴﾜﾀｧｷﾎ ｹﾝﾈｯ
     * @param year int
     * @param month int
     * @return int
     */
    public static int getHoursOfMonth(int year, int month) {
        try {
            long thisMonth = firstDateOfMonth(Integer.toString(year), Integer.toString(month)).getTime();
            long nextMonth = firstDateOfNextMonth(Integer.toString(year), Integer.toString(month)).getTime();
            return (int) ((nextMonth - thisMonth) / (1000 * 60 * 60));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * ﾇﾘｴ・dateﾀﾇ ｿｬｵｵｸｦ ｹﾝﾈｯ
     * @return
     * @param d
     */
    public static int getYear(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.YEAR);
    }

    /**
     * ﾇﾘｴ・dateﾀﾇ ﾁﾖﾂｦ ｹﾝﾈｯ
     * @return
     * @param d
     */
    public static int getWeekOfYear(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * ﾇﾘｴ・ｳ箏ｵｿ｡ ﾇﾘｴ酩ﾏｴﾂ ﾁﾖﾂﾇ ｿ菎ﾏ dateｸｦ ｹﾝﾈｯ
     * @return Date
     * @param weekOfYear
     * @param year
     */
    public static Date getMonday_weekOfYear(int year, int weekOfYear) {
        // java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat( "yyyy/MM/dd" );
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        return c.getTime();
    }

    /**
     * ﾇﾘｴ・ｳ箏ｵｿ｡ ﾇﾘｴ酩ﾏｴﾂ ﾁﾖﾂﾇ ﾅ菫菎ﾏ dateｸｦ ｹﾝﾈｯ
     * @return
     * @param weekOfYear
     * @param year
     */
    public static Date getSaturday_weekOfYear(int year, int weekOfYear) {
        // java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat( "yyyy/MM/dd" );
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

        return c.getTime();
    }

    /**
     * 2005-01-01 -> Dateｷﾎ ｹﾝﾈｯ
     * @return Date
     * @param str
     */
    public static Date parseDateStr(String str) {
        Calendar cal = Calendar.getInstance();
        str = str.trim();
        if (str.length() <= 0)
            return null;
        StringTokenizer st = new StringTokenizer(str, "-");
        int i = 0, year = 0, month = 0, date = 0, token = 0;
        while (st.hasMoreTokens()) {
            token = Integer.parseInt(st.nextToken());
            if (i == 0)
                year = token;
            else if (i == 1)
                month = token;
            else if (i == 2)
                date = token;
            i++;
        }
        cal.set(year, month - 1, date);

        return new Date(cal.getTimeInMillis());
    }

    /**
     * //yyyy/mm/ddﾇ・ﾂﾀﾇ Stringﾇ・ﾀﾏﾀﾚｸｦ Dateﾇ・ｸｷﾎ ｺｯﾈｯ
     * @return
     * @param str
     */
    public static Date parseSlashDateStr(String str) {
        Calendar cal = Calendar.getInstance();
        if (str.equals("//")) {
            return new Date(0);
        }

        str = str.trim();
        if (str.length() <= 0)
            return null;
        StringTokenizer st = new StringTokenizer(str, "/");
        int i = 0, year = 0, month = 0, date = 0, token = 0;
        while (st.hasMoreTokens()) {
            token = Integer.parseInt(st.nextToken());
            if (i == 1)
                month = token;
            else if (i == 0)
                date = token;
            else if (i == 2)
                year = token;
            i++;
            // System.out.println(token);
        }
        cal.set(year, month - 1, date);

        return new Date(cal.getTimeInMillis());
    }

    /**
     * ｿ・ｰ霆・+/-)
     * @return
     * @param diffMonth : ｱ篌ｻｿ・
     * @param srcMonth : ﾃﾟｰ｡ｿ・
     */
    public static String getAddMonth(String srcMonth, int diffMonth) {
        srcMonth = srcMonth.replaceAll("-", "");

        if (srcMonth.length() < 6)
            return "";

        int iY, iM;

        iY = Integer.parseInt(srcMonth.substring(0, 4), 10);
        iM = Integer.parseInt(srcMonth.substring(4, 6), 10);

        iM += diffMonth;

        if (iM > 12) {
            iY += Math.floor(iM / 12);
            iM = iM % 12;
        } else if (iM <= 0) {
            iM *= -1;
            iY -= Math.floor(iM / 12) + 1;
            iM = 12 - (iM % 12);
        }

        return "" + iY + (iM < 10 ? "0" : "") + iM;
    }

    public static String getDateByType(String type) {
        java.text.SimpleDateFormat getDate = new SimpleDateFormat(type, Locale.KOREA);
        String tmpDate = getDate.format(new java.util.Date());
        getDate = null; // ｼｿ null ﾃｳｸｮ.
        return tmpDate;
    }

    public static String getAgoMonth(String currDate, int agoMon, int agoDay) {
        Calendar c1 = Calendar.getInstance();

        int iCurrYear = Integer.parseInt(currDate.substring(0, 4));
        int iCurrMonth = Integer.parseInt(currDate.substring(4, 6));
        int iCurrDay = Integer.parseInt(currDate.substring(6, 8));
        // System.out.println(iCurrYear+"<>"+iCurrMonth+"<>"+iCurrDay);

        c1.set(iCurrYear, iCurrMonth - 1, iCurrDay);

        c1.add(Calendar.MONTH, agoMon);
        c1.add(Calendar.DAY_OF_MONTH, agoDay);

        String sAgoYear = "" + c1.get(Calendar.YEAR);
        String sAgoMonth = (c1.get(Calendar.MONTH) + 1) < 10 ? "0" + (c1.get(Calendar.MONTH) + 1) : "" + (c1.get(Calendar.MONTH) + 1);
        String sAgoDay = c1.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + c1.get(Calendar.DAY_OF_MONTH) : "" + c1.get(Calendar.DAY_OF_MONTH);

        String sAgoDate = sAgoYear + sAgoMonth + sAgoDay;
        // System.out.println(sAgoDate);

        return sAgoDate;
    }

    // int iYoil = Sunday:1 ~ Saturday:7
    public static String getYoIl(String currDate, int iYoil) {
        Calendar c1 = Calendar.getInstance();

        int iCurrYear = Integer.parseInt(currDate.substring(0, 4));
        int iCurrMonth = Integer.parseInt(currDate.substring(4, 6));
        int iCurrDay = Integer.parseInt(currDate.substring(6, 8));

        c1.set(iCurrYear, iCurrMonth - 1, iCurrDay);

        int iCurrYoil = 0;
        c1.add(Calendar.DAY_OF_MONTH, -1);
        for (int i = 0; i < 7; i++) {
            c1.add(Calendar.DAY_OF_MONTH, 1);
            iCurrYoil = c1.get(Calendar.DAY_OF_WEEK);
            if (iYoil == iCurrYoil) {
                break;
            }
        }

        String sYoilYear = "" + c1.get(Calendar.YEAR);
        String sYoilMonth = (c1.get(Calendar.MONTH) + 1) < 10 ? "0" + (c1.get(Calendar.MONTH) + 1) : "" + (c1.get(Calendar.MONTH) + 1);
        String sYoilDay = c1.get(Calendar.DAY_OF_MONTH) < 10 ? "0" + c1.get(Calendar.DAY_OF_MONTH) : "" + c1.get(Calendar.DAY_OF_MONTH);

        String sYoilDate = sYoilYear + sYoilMonth + sYoilDay;

        return sYoilDate;
    }

    /**
     * endDate가 startDate 보다 differDays 만큼 startDate :2007-10-01 endDate 2007-10-08 differDays 7
     * @param startDate
     * @param endDate
     * @param differDays
     * @return
     */
    public static boolean isDifferenceDate(Date startDate, Date endDate, int differDays) {
        if (differDays < 0) {
            return false;
        }
        if (startDate == null || endDate == null) {
            return false;
        }
        // Long differ = (endDate.getTime() - startDate.getTime())/(60 * 1000 * 60 * 24);
        Long differ = endDate.getTime() - startDate.getTime();

        int sec = (int) (differ / 1000);
        // System.out.println("sec : " + sec);
        int min = sec / 60;
        // System.out.println("min: " + min);
        int hour = min / 60;
        // System.out.println("hour: " + hour);
        int day = -1;

        if (hour > 24) {
            day = hour / 24;
        } else {
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = (Calendar) c1.clone();
            c1.setTime(startDate);
            c2.setTime(endDate);
            day = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
        }
        // System.out.println("days: " + day);
        if (day < 0)
            return false;
        return day <= differDays;
    }

    public static boolean isDifferenceDate(String startDateStr, String endDateStr, int differDays) {
        if (differDays < 0) {
            return false;
        }
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = DateUtil.getDateTime(startDateStr);
            endDate = DateUtil.getDateTime(endDateStr);
        } catch (ParseException e) {
            return false;
        }

        return isDifferenceDate(startDate, endDate, differDays);
    }


    /**
     * getCurrDate
     * @param dateFormat
     * @return
     */

    public static String getCurrDate(final String dateFormat) {
        String sYYMM = "";
        try {
            Calendar calendar = Calendar.getInstance();
            Date currDate = calendar.getTime();
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
            sYYMM = formatter.format(currDate);
        } catch (Exception e) {
        }
        return sYYMM;
    }
}
